//
//  LCTaskAdvertViewController.m
//  lechuang
//
//  Created by Mr_hi on 2023/4/14.
//  Copyright © 2023 Mooyi. All rights reserved.
//

#import "LCTaskAdvertViewController.h"

#import "LCCouponService.h"
#import "LCTaskService.h"
#import "LCCouponAdvertModel.h"

//轮播图
#import "HQFlowView.h"
#import "LCPageControlView.h"
//公告轮播
#import "SGAdvertScrollView.h"
#import "XWXAllXieYiController.h"

#import "LCMyTaskViewController.h"
#import "LCStudyViewController.h"
#import "LCAdInvestViewController.h"
#import "LCAppAgentViewController.h"
#import "LCSuperiorsViewController.h"

@interface LCTaskAdvertViewController ()<HQFlowViewDelegate,HQFlowViewDataSource>

//轮播
@property (weak, nonatomic) IBOutlet UIView *advertBgView;
@property (nonatomic, strong) LCPageControlView *advertPageC;
@property (strong, nonatomic) HQFlowView *adFlowView;
//轮播图数据
@property (strong, nonatomic)NSArray * advertArr;

@property (weak, nonatomic) IBOutlet XWXImgTitleBtn *myTaskBtn;//任务进度
@property (weak, nonatomic) IBOutlet XWXImgTitleBtn *studyBtn;//学习中心
@property (weak, nonatomic) IBOutlet XWXImgTitleBtn *cooperateBtn;//投放申请
@property (weak, nonatomic) IBOutlet XWXImgTitleBtn *contactBtn;//推广申请

//公告
@property (weak, nonatomic) IBOutlet UIView *noticeBgView;
@property (strong, nonatomic)SGAdvertScrollView * noticeView;
//公告数据
@property (strong, nonatomic)NSArray * noticeArr;


@end

@implementation LCTaskAdvertViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
    
    [self getAdVertData];
    [self getNoticeData];
}

-(void)initUI {
    [self.advertBgView addSubview:self.adFlowView];
    [self.advertBgView addSubview:self.advertPageC];
    
    [self.adFlowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.left.bottom.equalTo(self.advertBgView);
    }];
    [self.advertPageC mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(16);
        make.right.left.bottom.equalTo(self.advertBgView);
    }];
    
    self.myTaskBtn.Space = 7;
    self.myTaskBtn.BtnStyle = XWXBtnStyleImageUp;
    self.studyBtn.Space = 7;
    self.studyBtn.BtnStyle = XWXBtnStyleImageUp;
    self.cooperateBtn.Space = 7;
    self.cooperateBtn.BtnStyle = XWXBtnStyleImageUp;
    self.contactBtn.Space = 7;
    self.contactBtn.BtnStyle = XWXBtnStyleImageUp;
    
    [self.noticeBgView addSubview:self.noticeView];
    [self.noticeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.top.bottom.equalTo(self.noticeBgView);
    }];
}

-(HQFlowView *)adFlowView {
    if (!_adFlowView) {
        _adFlowView = [[HQFlowView alloc] init];
        _adFlowView.delegate = self;
        _adFlowView.dataSource = self;
        _adFlowView.orginPageCount = 2;
        _adFlowView.isOpenAutoScroll = YES;
        _adFlowView.autoTime = 3.0;
        _adFlowView.orientation = HQFlowViewOrientationHorizontal;
    }
    return _adFlowView;
}

-(LCPageControlView *)advertPageC {
    if (!_advertPageC) {
        _advertPageC = [[LCPageControlView alloc] init];
        _advertPageC.otherDotColor = [UIColor colorWithHexString:@"#000000" alpha:0.2];
        _advertPageC.currentDotColor = [UIColor colorWithHexString:@"#000000" alpha:0.2];
        _advertPageC.dotSpace = 5;
        _advertPageC.dotHeight = 4;
        _advertPageC.otherDotWidth = 4;
        _advertPageC.currentDotWidth = 16;
        _advertPageC.cornerRadius = 2;
        
        self.adFlowView.pageControl.currentPage = 0;
        self.adFlowView.pageControl = _advertPageC;
    }
    return _advertPageC;
}

-(SGAdvertScrollView *)noticeView {
    if (!_noticeView) {
        NSString *str = kFORMAT(@"暂无公告");
        _noticeView = [[SGAdvertScrollView alloc] initWithFrame:CGRectZero];
        _noticeView.titleFont = [UIFont systemFontOfSize:13];
        _noticeView.titleColor = XWXPrimaryColor(@"#222222");
        _noticeView.titles = @[str,str];
    }
    return _noticeView;
}

- (void)getAdVertData {
    [LCCouponService getAdvertWithAdvertname:@"任务栏焦点图" Block:^(BOOL isSuccess, NSArray<LCCouponAdvertModel *> *list) {
        if (isSuccess) {
            self.advertArr = list;
            self.adFlowView.orginPageCount = self.advertArr.count;
            [self.adFlowView reloadData];
        }
    }];
}

-(void)getNoticeData {
    [LCTaskService getNoticeDataWithBlock:^(BOOL isSuccess, NSArray<LCNoticeModel *> *list) {
        if (isSuccess) {
            self.noticeArr = list;
            if (self.noticeArr.count) {
                if (self.noticeArr.count == 1) {
                    LCNoticeModel * notice = [self.noticeArr firstObject];
                    self.noticeView.titles = @[notice.strcontext,notice.strcontext];
                } else {
                    NSMutableArray * titleArr = [NSMutableArray array];
                    for (LCNoticeModel * notice in self.noticeArr) {
                        [titleArr addObject:notice.strcontext];
                    }
                    self.noticeView.titles = titleArr;
                }
            }
        }
    }];
}

#pragma mark JQFlowViewDelegate
- (CGSize)sizeForPageInFlowView:(HQFlowView *)flowView {
    return CGSizeMake(ScreenW-34, 156*proW);
}

- (void)didSelectCell:(UIView *)subView withSubViewIndex:(NSInteger)subIndex {
    LCCouponAdvertModel * advert = self.advertArr[subIndex];
    if ([NSString urlValidation:advert.strlinks]) {
        XWXAllXieYiController *webVC = [XWXAllXieYiController new];
        webVC.tileStr = advert.listname;
        webVC.urlStr = advert.strlinks;
        webVC.isPushFrom = YES;
        webVC.hidesBottomBarWhenPushed = YES;
        [self.parentVC.navigationController pushViewController:webVC animated:YES];
    }
}

#pragma mark JQFlowViewDatasource
- (NSInteger)numberOfPagesInFlowView:(HQFlowView *)flowView {
    return self.advertArr.count;
}

- (HQIndexBannerSubview *)flowView:(HQFlowView *)flowView cellForPageAtIndex:(NSInteger)index {
    HQIndexBannerSubview *bannerView = (HQIndexBannerSubview *)[flowView dequeueReusableCell];
    if (!bannerView) {
        bannerView = [[HQIndexBannerSubview alloc] initWithFrame:CGRectMake(0, 0, self.adFlowView.frame.size.width, self.adFlowView.frame.size.height)];
        bannerView.layer.cornerRadius = 5;
        bannerView.layer.masksToBounds = YES;
        bannerView.coverView.backgroundColor = [UIColor darkGrayColor];
    }
    LCCouponAdvertModel * advert = self.advertArr[index];
    [bannerView.mainImageView sd_setImageWithURL:[LCFunction XWX_URLImg:advert.strimage]];
    return bannerView;
}

- (void)didScrollToPage:(NSInteger)pageNumber inFlowView:(HQFlowView *)flowView {
    [self.adFlowView.pageControl setCurrentPage:pageNumber];
}

- (IBAction)functionBtnAction:(XWXImgTitleBtn *)sender {
    [self pushToControllerWithName:sender.titleLabel.text];
}

-(void)pushToControllerWithName:(NSString *)name {
    if ([name isEqualToString:@"项目进度"]) {
        LCMyTaskViewController * pushVC = [LCMyTaskViewController xx_instantiateFromStoryboardNamed:@"TaskFunction"];
        [self.navigationController pushViewController:pushVC animated:YES];
    } else if ([name isEqualToString:@"学习中心"]) {
        LCStudyViewController * pushVC = [LCStudyViewController xx_instantiateFromStoryboardNamed:@"TaskFunction"];
        [self.navigationController pushViewController:pushVC animated:YES];
    } else if ([name isEqualToString:@"广告主申请"]) {
        LCAdInvestViewController * pushVC = [LCAdInvestViewController xx_instantiateFromStoryboardNamed:@"TaskFunction"];
        [self.navigationController pushViewController:pushVC animated:YES];
    } else if ([name isEqualToString:@"合作申请"]) {
        LCAppAgentViewController * pushVC = [LCAppAgentViewController xx_instantiateFromStoryboardNamed:@"TaskFunction"];
        [self.navigationController pushViewController:pushVC animated:YES];
    } else if ([name isEqualToString:@"联系上级"]) {
        LCSuperiorsViewController * pushVC = [LCSuperiorsViewController xx_instantiateFromStoryboardNamed:@"TaskFunction"];
        [self.navigationController pushViewController:pushVC animated:YES];
    }
}

@end
